﻿using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using System.Collections.Concurrent;

namespace DotNetty.Extensions
{
    public class TcpSocketConnection
    {
        public TcpSocketConnection(IChannel channel)
        {
            Channel = channel;
            Id = Channel.Id.AsShortText();
        }

        public IChannel Channel { get; }

        public string Id { get; }

        public string Name { get; set; }

        public ConcurrentDictionary<string, object> SessionItems { get; } = [];

        public async Task WriteAsync(object message)
        {
            await Channel.WriteAsync(message);
        }

        public async Task WriteAsync(IByteBuffer buffer)
        {
            await Channel.WriteAsync(buffer);
        }

        public async Task WriteAsync(byte[] bytes)
        {
            await Channel.WriteAsync(Unpooled.WrappedBuffer(bytes));
        }

        public void Flush()
        {
            Channel.Flush();
        }

        public async Task WriteAndFlushAsync(object message)
        {
            await Channel.WriteAndFlushAsync(message);
        }

        public async Task WriteAndFlushAsync(IByteBuffer buffer)
        {
            await Channel.WriteAndFlushAsync(buffer);
        }

        public async Task WriteAndFlushAsync(byte[] bytes)
        {
            await Channel.WriteAndFlushAsync(Unpooled.WrappedBuffer(bytes));
        }

        public async Task CloseAsync()
        {
            await Channel.CloseAsync();
        }
    }
}
